﻿using System;
using System.Collections.Generic;
using TechMadeira.Data;

namespace TechMadeira.Workflows
{
    public class Factory : IFactory
    {
        public Factory( IFactory aFactory )
        {
            InternalFactory = aFactory;
        }

        public IFactory InternalFactory { get; private set; }

        public object CreateObject( string aType )
        {
            if ( aType != "Order" ) return InternalFactory.CreateObject( aType );
            return new OrderWithStatus( InternalFactory.CreateObject( aType ) as IOrder );
        }

        public T CreateObject<T>()
        {
            var obj = InternalFactory.CreateObject<T>();
            if ( obj is IOrder ) obj= Cast<T>( new OrderWithStatus( obj as IOrder ) ) ;
            return obj;
        }

        public Type[] GetTypes()
        {
            var types = new List<Type>( InternalFactory.GetTypes() );
            types.Add( typeof(OrderWithStatus) );
            return types.ToArray();
        }

        public static T Cast<T>( object o )
        {
            return (T) o;
        }


    }
}
